const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatDateTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1

  return [year, month].map(formatNumber).join('-')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const Verify = {
  /**
   * 验证是否为空
   * @param value
   * @returns
   */
  isValidNull: function (value) {
    return typeof value != 'undefined' && value != "undefined" && value != undefined && value != 'null' && value != "null" && value != null && value != "" && value != '' ? true : false;
  },
  /**
   * 验证是否为字母
   * @param value
   * @returns
   */
  isValidLetter: function (value) {
    var re = /^[A-Za-z]+$/;
    return re.test(value);
  },
  /**
   * 验证是否是手机号码
   * @param value
   * @returns
   */
  isCellPhoneNumber: function (value) {
    var re = /^1[3|4|5|7|8][0-9]\d{4,8}$/;
    return re.test(value);
  },
  /**
   * 验证是否是百分比
   * @param value
   * @returns
   */
  isValidPercent: function (value) {
    var re = /^([1-9][0-9]?(\.[0-9]{1,2})?)$|^(0\.[1-9][0-9]?)$|^(0\.[0-9][1-9])$|^100(\.00)?$/;
    return re.test(value);
  },
  /**
   * 验证是否是金额
   * @param value
   * @returns boolean
   */
  isValidMoney: function (value) {
    var re = /^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/;
    return re.test(value);
  },
  /**
   * 验证是否是数字
   * @param value
   * @returns
   */
  isNumber: function (value) {
    var re = /^[0-9]*$/;
    return re.test(value);
  },
  /**
   * 验证邮箱是否为正确格式
   * @param value
   * @returns
   */
  isEmail: function (value) {
    var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return re.test(value);
  },
  /**
   * 简单版身份证验证
   * @param value
   * @returns {boolean}
   */
  isIdentity: function (value) {
    var re = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    return re.test(value);
  },

  /**
   * 密码验证
   * @param value
   * @returns {boolean}
   */
  isPassword: function (value) {
    var re = /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9])|(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9])|(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])).{6,}|(?:(?=.*[A-Z])(?=.*[a-z])|(?=.*[A-Z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[^A-Za-z0-9])|(?=.*[a-z])(?=.*[0-9])|(?=.*[a-z])(?=.*[^A-Za-z0-9])|(?=.*[0-9])(?=.*[^A-Za-z0-9])|).{6,}$/
    return re.test(value);
  },

  /**
   * 邮箱验证
   * @param value
   * @returns {boolean}
   */
  isEmail2: function (value) {
    var re = /^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/;
    return re.test(value);
  }
}
module.exports = {
  isValidNull: Verify.isValidNull,
  isValidLetter: Verify.isValidLetter,
  isCellPhoneNumber: Verify.isCellPhoneNumber,
  isValidPercent: Verify.isValidPercent,
  isValidMoney: Verify.isValidMoney,
  isNumber: Verify.isNumber,
  isEmail: Verify.isEmail,
  isIdentity: Verify.isIdentity,
  isPassword: Verify.isPassword,
  isEmail2: Verify.isEmail2,
  formatTime: formatTime,
  formatDateTime: formatDateTime
}
